﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SetVersionNumber.FileWriters;
using System.Reflection;
using SetVersionNumber.VersionStrategies;

namespace SetVersionNumberTests.FileWriters {

	/// <summary>
	/// All of the unit tests for the <c>NuGestFileWriter</c>.
	/// </summary>
	[TestClass]
	public class NuGetFileWriterTests {

		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		#region WillProcessFile
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		/// <summary>
		/// We want to test if the regular expression matches a .nuspec file.
		/// </summary>
		[TestMethod]
		public void WillProcessFileMatchesOnNuSpecFiles( ) {
			var sut = new NuGetFileWriter();
			var willProcess = sut.WillProcessFile(@"c:\apple\pear\Testing.nuspec");
			Assert.IsTrue(willProcess, "The file should have been a match");
		}


		/// <summary>
		/// We want to test if the regular expression does NOT match a non .nuspec file.
		/// </summary>
		[TestMethod]
		public void WillProcessFileDoesNotMatchANonNuSpecFiles( ) {
			var sut = new NuGetFileWriter();
			var willProcess = sut.WillProcessFile(@"c:\apple\pear\Testing.nusecp");
			Assert.IsFalse(willProcess, "The file should have been a match");
		}

		#endregion



		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		#region
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		[TestMethod]
		public void TestProcessFileCorrectlySetsVersionNumberInFile( ) {
			// Load the input and expected files
			var input = HelperClass.GetEmbeddedResource(STREAM_NAME + "NuGetFileWriterTests.input.nuspec");
			var expected = HelperClass.GetEmbeddedResource(STREAM_NAME + "NuGetFileWriterTests.expected.nuspec");

			// Write out the input to a temporary file
			var tempFile = Path.GetTempFileName();
			File.WriteAllText(tempFile, input);

			// Create and execute our SUT
			ExplicitVersionStrategy.UserDefinedVersionNumber = "2.1.812";
			var strategy = new ExplicitVersionStrategy();
			var sut = new NuGetFileWriter();
			sut.ProcessFile(tempFile, strategy);

			// Now check that the file was processed correctly
			var actual = File.ReadAllText(tempFile);
			Assert.AreEqual(expected, actual, "The NuGet file writer did not process the file correctly");
		}
		const string STREAM_NAME = "SetVersionNumberTests.FileWriters.";

		#endregion

	}//class

}//namespace
